package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjWorkOrder;
import com.zb.entity.ZjWorkTotalFeeFreeze;
import com.zb.mapper.ZjWorkTotalFeeFreezeMapper;
import com.zb.service.ZjFactoryService;
import com.zb.service.ZjWorkTotalFeeFreezeService;
import com.zb.utils.ZbOrderNoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 工单总费用冻结占用释放表，与工单一对一 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-04-07
 */
@Slf4j
@Service
public class ZjWorkTotalFeeFreezeServiceImpl extends ServiceImpl<ZjWorkTotalFeeFreezeMapper, ZjWorkTotalFeeFreeze> implements ZjWorkTotalFeeFreezeService {

    // 解冻/释放占用
    @Override
    public void saveUnfreeze(Long custAccId, Long factoryId, Long workId) {
        if (null == factoryId || factoryId == 0) {
            return;
        }
        ZjWorkTotalFeeFreeze freeze = new ZjWorkTotalFeeFreeze();
        freeze.setId(workId);
        freeze.setUnfreezeCode(ZbOrderNoUtil.unfreezeNo(custAccId));
        freeze.setUnfreeze(true);
        freeze.setUnfreezeTime(new Date());
        this.updateById(freeze);
    }

    // 解冻/释放占用
    @Override
    public void saveUnfreeze(Long custAccId, List<ZjWorkOrder> orderList) {
        if (CollectionUtil.isEmpty(orderList)) {
            return;
        }
        List<ZjWorkTotalFeeFreeze> freezeList = new ArrayList<>();
        ZjWorkTotalFeeFreeze freeze = null;
        for (ZjWorkOrder order : orderList) {
            freeze = new ZjWorkTotalFeeFreeze();
            freeze.setId(order.getId());
            freeze.setUnfreezeCode(ZbOrderNoUtil.unfreezeNo(custAccId));
            freeze.setUnfreeze(true);
            freeze.setUnfreezeTime(new Date());
            freezeList.add(freeze);
        }
        this.updateBatchById(freezeList);
    }
}
